package com.boot.app.base.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boot.api.ApiResult;
import com.boot.app.base.dto.PasswordDto;
import com.boot.app.base.dto.UserDTO;
import com.boot.app.base.eo.UserEo;
import com.boot.app.base.service.IBaseUserDetailsService;
import com.boot.app.base.service.IBaseUserService;
import com.boot.app.base.vo.UserVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

@Tag(name = "BaseUserController", description = "用户管理")
@RestController
@RequestMapping("base/user")
public class BaseUserController {

    private final IBaseUserService service;

    private final IBaseUserDetailsService userDetailsService;

    @Autowired
    public BaseUserController(
        IBaseUserService service,
        IBaseUserDetailsService userDetailsService
    ){
        this.service = service;
        this.userDetailsService = userDetailsService;
    }

    @Operation(summary = "获取用户")
    @GetMapping("{id}")
    public ApiResult<UserVO> getUserVO(@PathVariable("id") long id){
        UserVO userVo = service.getUserVOById(id);
        return ApiResult.success(userVo);
    }

    @Operation(summary = "用户列表")
    @GetMapping("page/{page}/{size}")
    public ApiResult<Page<UserVO>> getUserPage(@PathVariable int page, @PathVariable int size, UserDTO userDto){
        Page<UserVO> userVOPage = service.pageUserVO(new Page<>(page, size), userDto);
        return ApiResult.success(userVOPage);
    }

    @Operation(summary = "新增/修改用户")
    @PostMapping()
    public ApiResult<?> postOrPutUser(@Validated @RequestBody UserDTO userDto){
        userDetailsService.saveOrUpdateUserDto(userDto);
        return ApiResult.success();
    }

    @Operation(summary = "删除用户")
    @DeleteMapping("{id}")
    public ApiResult<?> deleteUser(@PathVariable("id") long id){
        service.removeUserById(id);
        return ApiResult.success();
    }

    @Operation(summary = "修改密码")
    @PutMapping("password")
    public ApiResult<?> putPassword(@Validated @RequestBody PasswordDto passwordDto){
        userDetailsService.updatePassword(passwordDto);
        return ApiResult.success();
    }

    @Operation(summary = "重置密码")
    @PutMapping("password/reset/{id}")
    public ApiResult<?> resetPassword(@PathVariable long id){
        userDetailsService.resetPassword(id);
        return ApiResult.success();
    }

    @GetMapping("export")
    public void exportUserList(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=User.xlsx");
        ArrayList<UserEo> userEoList = new ArrayList<>();
        UserEo userEo = new UserEo();
        userEo.setUsername("aaa");
        userEoList.add(userEo);
        EasyExcel.write(response.getOutputStream(), UserEo.class).autoCloseStream(Boolean.FALSE).sheet().doWrite(userEoList);
    }

}
